package datastructure.impl;

import java.lang.reflect.Array;

import com.sun.org.apache.bcel.internal.generic.RETURN;

import datastructure.Queue;

/**
 * LoopQueue
 * @author Jun
 *
 * @param <T>
 */
public class SeqQueue<T> implements Queue<T> {
		
	/**
	 * Capacity of Array
	 */
	private int MAXNUM;
	
	/**
	 *  Front index in the queue
	 */
	private int f;
	/**
	 *  possition need to insert
	 */
	private int r;
	
	// one node is reserved for distinguish between blank queue and full queue.
	private T[] q;
	
	/**
	 * 
	 */
	@SuppressWarnings("unchecked")
	public SeqQueue(Class e) {
		
		super();
		MAXNUM = 100;
		f = 0;
		r = 0;
		q = (T[])Array.newInstance(e, MAXNUM);		
		
	}
	
	@Override
	public void DeQueue() {
		
		// TODO Auto-generated method stub
		if(f == r){
			System.out.println("UnderFlow Queue!");
			return;
		}		
		
		f = (f+1)%MAXNUM;
		
	}
	@Override
	public void enQueue(T elem) {
		// TODO Auto-generated method stub
		if((r+1)%MAXNUM==f){
			System.out.println("Overflow Queues!");
			return;
		}
		else{
			q[r] = elem;
			r = (r+1)%MAXNUM;	
		}
		
	}
	@Override
	public T frontQueue() {
		// TODO Auto-generated method stub
		if(f == r)
				return null;
		
		return q[f];
		
	}
	@Override
	public boolean isEmptyQueue() {
		// TODO Auto-generated method stub
		return (f==r);
	}
}
